using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Net;

public class Data
{
	//sound
	static public float volume;
	static public int sound;
	static public int music;
	
	//gamemode
	static public string difficult;
	static public string gameMode = BoardLocal.MODE_COMBO_HUNT;
    static public bool multiplayer;
    static public AutoServer gameServer;
    static public AutoClient gameClient;
	static public List<KeyValuePair<IPEndPoint, Player.PlayerInfo>> players;
	
	//scores
	static public string[] arrayOfName = new string[5];
	static public int[] arrayOfScore = new int[5];

	static public void NewHighScore(string playerName, int score)
	{
		Data.LoadData();

        bool flag = false;

        for (int i=0; i<arrayOfScore.Length; i++)
        {
            if (arrayOfScore[i] == 0)
            {
                arrayOfScore[i] = score;
                arrayOfName[i] = playerName;
   
                SwitchFunc(playerName, score, i);

                flag = true;

                break;
            }
        }

        if (!flag)
        {
            int minimum = arrayOfScore[0];
            for (int i=0; i<arrayOfScore.Length; i++)
            {
                if (arrayOfScore[i] < minimum)
                {
                    minimum = arrayOfScore[i];
                }
            }

            for (int i=0; i<arrayOfScore.Length; i++)
            {
                if (arrayOfScore[i] == minimum & minimum < score)
                {
                    arrayOfScore[i] = score;
                    arrayOfName[i] = playerName;

                    SwitchFunc(playerName, score, i);

                    break;
                }
            }
        }

        Data.SaveData();
	}

	static public void LoadData()
	{
		//sound
		volume = PlayerPrefs.GetFloat("volume", 1.0f);
		sound = PlayerPrefs.GetInt("sound", 1);
		music = PlayerPrefs.GetInt("music", 1);
		
		//gamemode
		difficult = PlayerPrefs.GetString("difficult", "Easy");
		gameMode = PlayerPrefs.GetString("gameMode", "ComboHunt");

		//scores
		arrayOfName[0] = PlayerPrefs.GetString("firstPlaceName", "Name");
		arrayOfScore[0] = PlayerPrefs.GetInt("firstPlaceScore", 0);
				
        arrayOfName[1] = PlayerPrefs.GetString("secondPlaceName", "Name");
		arrayOfScore[1] = PlayerPrefs.GetInt("secondPlaceScore", 0);
				
        arrayOfName[2] = PlayerPrefs.GetString("thirdPlaceName", "Name");
		arrayOfScore[2] = PlayerPrefs.GetInt("thirdPlaceScore", 0);
				
        arrayOfName[3] = PlayerPrefs.GetString("fourthPlaceName", "Name");
		arrayOfScore[3] = PlayerPrefs.GetInt("fourthPlaceScore", 0);
				
        arrayOfName[4] = PlayerPrefs.GetString("fifthPlaceName", "Name");
		arrayOfScore[4] = PlayerPrefs.GetInt("fifthPlaceScore", 0);
	}
	
	static public void SaveData()
	{
		PlayerPrefs.Save();
	}

    static void SwitchFunc(string playerName, int score, int i)
    {
        switch (i)
        {
            case 0:
            {
                PlayerPrefs.SetString("firstPlaceName", playerName);
                PlayerPrefs.SetInt("firstPlaceScore", score);
                break;
            }
            case 1:
            {
                PlayerPrefs.SetString("secondPlaceName", playerName);
                PlayerPrefs.SetInt("secondPlaceScore", score);
                break;
            }
            case 2:
            {
                PlayerPrefs.SetString("thirdPlaceName", playerName);
                PlayerPrefs.SetInt("thirdPlaceScore", score);
                break;
            }
            case 3:
            {
                PlayerPrefs.SetString("fourthPlaceName", playerName);
                PlayerPrefs.SetInt("fourthPlaceScore", score);
                break;
            }
            case 4:
            {
                PlayerPrefs.SetString("fifthPlaceName", playerName);
                PlayerPrefs.SetInt("fifthPlaceScore", score);
                break;
            }
        }
    }

    static public void Sort()
    {
        int temp = 0;

        for (int k = arrayOfScore.Length - 1; k > 0; k--)
            for (int i = 0; i < k; i++)
                if (arrayOfScore[i] < arrayOfScore[i + 1])
                {
                    temp = arrayOfScore[i];
                    arrayOfScore[i] = arrayOfScore[i + 1];
                    arrayOfScore[i + 1] = temp;

                    string tempStr = arrayOfName[i];
                    arrayOfName[i] = arrayOfName[i + 1];
                    arrayOfName[i + 1] = tempStr;
                }
    }
}
